package com.erdos.upc.dao.ibatis;

import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;

import com.erdos.common.dao.IBatisGenericDaoImpl;
import com.erdos.common.exception.DaoException;
import com.erdos.core.entity.Facility;
import com.erdos.upc.dao.DataPrivilegeDao;
import com.erdos.upc.entity.BusinessPrivilege;


public class DataPrivilegeDaoImpl extends IBatisGenericDaoImpl<BusinessPrivilege> implements DataPrivilegeDao {

	/**
	 * 删除角色与数据权限的关联
	 * @param dataPrivilegeId
	 * @author jl
	 * @since  2008-8-11 
	 */
	public void deleteDataPrivilegeRole(Long dataPrivilegeSId) {
		this.getSqlMapClientTemplate().delete("DataPrivilege.deleteByDataPrivilegeSId", dataPrivilegeSId);		
	}

	/* (non-Javadoc)
	 * @see com.hp.upc.dao.DataPrivilegeDao#findByUserParams(java.util.Map)
	 */
	@SuppressWarnings("unchecked")
	public List<BusinessPrivilege> findByUserParams(Map<String, Object> params) {
		try {
			return (List<BusinessPrivilege>) this.getSqlMapClientTemplate().
						queryForList("DataPrivilege.findByUserParams", params);
		} catch (DataAccessException e) {
			throw new DaoException("DataPrivilege.findByUserParams", new String[] { entityClass.getName(),params.toString()},e);
		}
	}

	/* (non-Javadoc)
	 * @see com.hp.upc.dao.DataPrivilegeDao#delete(java.lang.Long)
	 */
	public void delete(Long dataPrivilegeSId) {
		this.getSqlMapClientTemplate().delete("DataPrivilege.delete", dataPrivilegeSId);				
	}

	/* (non-Javadoc)
	 * @see com.hp.upc.dao.DataPrivilegeDao#insertDataPrivilegeRole(java.lang.Long, java.lang.Long)
	 */
	/*public void insertDataPrivilegeRole(Long dataPrivilegeSId, SecretRole role, Long controlType) {
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("roleSid", role.getSid());
		conditions.put("dataPrivilegeSid",dataPrivilegeSId);
		conditions.put("controlType",controlType);
		conditions.put("createdBy", role.getCreatedBy());
		conditions.put("createdDt", role.getCreatedDt());
		conditions.put("version", role.getVersion());
		conditions.put("updatedBy", role.getUpdatedBy());
		conditions.put("updatedDt",role.getUpdatedDt());
		this.getSqlMapClientTemplate().insert("DataPrivilege.insertDataPrivilegeRole", conditions);	
	}*/
	
	/**
	 * 临时方法，取全部facility
	 * @author jl
	 * @since  2008-8-13 
	 */
	@SuppressWarnings("unchecked")
	public List<Facility> findFacility() {
		try {
			return (List<Facility>)this.getSqlMapClientTemplate().queryForList("DataPrivilege.findFacility");
		} catch (DataAccessException e) {
			throw new DaoException("DataPrivilege.findFacility", new String[] { entityClass.getName()},e);
		}		
	}

	/* (non-Javadoc)
	 * @see com.hp.upc.dao.DataPrivilegeDao#findPrivilegeExist(java.util.Map)
	 */
	@SuppressWarnings("unchecked")
	public boolean findPrivilegeExist(Map<String, Object> params) {
		boolean existFlag = false;
		List<BusinessPrivilege> businessPrivileges = (List<BusinessPrivilege>)this.getSqlMapClientTemplate().queryForList("DataPrivilege.findPrivilegeExist",params);
		if(businessPrivileges!=null&&businessPrivileges.size()>0) {
			existFlag = true;
		}
		return existFlag;
	}

}
